/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.Mains;

import java.awt.Toolkit;

import org.apache.log4j.*;

import application.Forms.JFrame_Form;
import application.Arguments.ApplicationArguments;
import library.RemoteControl.*;
import library.Thread.*;
import library.Configuration.*;
import library.JpegNodes.*;


/**
 * The Main_Application class containg the static main method.
 * 
 * @author JNC
 */
public class Main_Application {
    /**
     * The log4j log
     */
    private static final Logger _logger = Logger.getLogger(Main_Application.class);
    
    /**
     * The static main method.
     * Tests for graphical capability, and then run the application
     * in the appropriate mode
     * @param args
     */
    public static void main(String args[]) {

        // process the arguments as the name of the config file must be given as argument
        // ------------------------------------------------------------------------------
        ApplicationArguments.processAllArguments(args);
        
        // call the appropriate method depending if the target has graphical capabilities
        // ------------------------------------------------------------------------------
        if (hasGraphicalCapabilities()) {
            _logger.info("Graphical capability detected");
            runWithGraphicalCapability();
        } else {
            _logger.info("No graphical capability detected");
            runWithoutGraphicalCapability();
        }
        
        _logger.debug("End of Main_Application.main");
    }
    
    /**
     * Tests graphical capability of the JVM by trying to get the default toolkit
     * in case of failure; the JVM is declared graphical non capable
     * @return true if graphical capable, false otherwise
     */
    public static boolean hasGraphicalCapabilities() {
        boolean bRet = true;
        
        try {
            Toolkit.getDefaultToolkit();
        }
        catch(java.lang.Throwable event) {
            bRet = false;
        }
        
        return bRet;
    }
    
    /**
     * Main processing when the JVM has graphical capabilities
     */
    private static void runWithGraphicalCapability() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                boolean mustEnd ;
                JFrame_Form mainFrame; // the main frame
        
                mustEnd = false;
                mainFrame = null;
                
                try {
                    mainFrame = new JFrame_Form();
                // the remote control is created in the constructor of JFrame_Form
                }
                catch(UnknownClassException exception) {
                    _logger.error("Exception_UnknownClassException caught while registering JFrame_Form (Interface_Form) : " + exception);
                    mustEnd = true;
                }

                if (! mustEnd) {
                    //sizes the JFrame accordingly to its content
                    mainFrame.pack();
                
                    // shows the window
                    mainFrame.setVisible(true);                    
                }
            }
            });
    }  
    
    /**
     * Main processing when the JVM has NO graphical capability
     */
    private static void runWithoutGraphicalCapability() {
        
        
        try {
            new ProcessingThread(Configuration.getPathProperty("NoForm.DefaultDirectory"),
                                 ActionTypeEnum.Rename,
                                 Configuration.getStringProperty("NoForm.IsRecursive").equals("True"),
                                 new VoidRemoteControl()).start();
        }
        catch(UnknownClassException exception) {
            _logger.error("Exception Exception_UnknownClassException caught while creating Class_processingThread : " + exception);
        }
    }
}